//
//  PageController.m
//  收纳
//
//  Created by  on 2022/10/26.
//

#import "PageController.h"
#import "AllPage.h"
#import "MinePage.h"
#import "HomePage.h"
#import "PageHeader.h"
#import "AddViewController.h"
#import "SearchViewController.h"
@interface PageController ()
@property (strong, nonatomic) IBOutlet UIView *toolBar;
@property (strong, nonatomic) IBOutlet UICollectionView *collection;
@property (strong, nonatomic) IBOutlet UIButton *homeBtn;
@property (strong, nonatomic) IBOutlet UIButton *allBtn;
@property (strong, nonatomic) IBOutlet UIButton *mineBtn;
@property (nonatomic, strong) NSMutableArray<Goods*>*array;
@end

@implementation PageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}
- (void)initUI{
    self.backBtn.enabled = NO;
    [self.backBtn setImage:nil forState:UIControlStateNormal];
    [self.backBtn setImage:nil forState:UIControlStateDisabled];
    [self.backBtn setTitle:LS(@"我的物品") forState:UIControlStateNormal];
    [self.rightBtn setTitle:@"" forState:UIControlStateNormal];
    [self.rightBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [self.collection registerNib:[AllPage nib] forCellWithReuseIdentifier:[AllPage ID]];
    [self.collection registerNib:[MinePage nib] forCellWithReuseIdentifier:[MinePage ID]];
    [self.collection registerNib:[HomePage nib] forCellWithReuseIdentifier:[HomePage ID]];
    [self.collection registerNib:[PageHeader nib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[PageHeader ID]];
}
- (void)loadData{
    [GoodsTool find:YES complete:^(NSMutableArray<Goods *> * _Nonnull result) {
        self.array = [NSMutableArray arrayWithArray:result];
        [self.collection reloadData];
        if (self.homeBtn.selected) {
            [self actionForBtn:self.homeBtn];
        }else if (self.allBtn.selected){
            [self actionForBtn:self.allBtn];
        }else if (self.mineBtn.selected){
            [self actionForBtn:self.mineBtn];
        }
    }];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    sender.selected = YES;
    if (sender.tag == 201) {
        AddViewController *vc = [AddViewController new];
        vc.model = [Goods new];
        vc.model.isContainer = NO;
        vc.model.stamp = [[NSDate currentTimeStamp:@"yyyy-MM-dd HH:mm:ss"] integerValue];
        [self push:vc :YES];
    }else if (sender.tag == 202){
        self.rightBtn.hidden = NO;
        self.homeBtn.selected = YES;
        self.allBtn.selected = self.mineBtn.selected = NO;
        [self.backBtn setTitle:LS(@"我的物品") forState:UIControlStateNormal];
        [self.collection setContentOffset:CGPointMake(SCREEN_WIDTH*(self.array.count<=0?0:1), 0) animated:NO];
    }else if (sender.tag == 203){
        self.rightBtn.hidden = NO;
        self.allBtn.selected = YES;
        self.homeBtn.selected = self.mineBtn.selected = NO;
        [self.backBtn setTitle:LS(@"物品") forState:UIControlStateNormal];
        [self.collection setContentOffset:CGPointMake(SCREEN_WIDTH*2, 0) animated:NO];
    }else if (sender.tag == 204){
        self.rightBtn.hidden = YES;
        self.mineBtn.selected = YES;
        self.homeBtn.selected = self.allBtn.selected = NO;
        [self.backBtn setTitle:LS(@"设置") forState:UIControlStateNormal];
        [self.collection setContentOffset:CGPointMake(SCREEN_WIDTH*3, 0) animated:NO];
    }
}
- (void)rightBtnAction{
    SearchViewController *vc = [SearchViewController new];
    [self push:vc :YES];
}
- (void)addNotify{
    [NotifyTool add:self :@selector(loadData) :notifyGoodsUpdate];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        HomePage * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomePage ID] forIndexPath:indexPath];
        cell.array = self.array;
        return  cell;
    }else if (indexPath.row == 1){
        AllPage * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[AllPage ID] forIndexPath:indexPath];
        cell.array = self.array;
        return  cell;
    }else{
        MinePage * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[MinePage ID] forIndexPath:indexPath];
        cell.array = self.array;
        return  cell;
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    PageHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[PageHeader ID] forIndexPath:indexPath];
    return  header;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(SCREEN_WIDTH,SCREEN_HEIGHT - NavHeight - 104);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH,100);
}

@end
